<?php

class AttackController extends Zend_Controller_Action
{
    public function init()
    {
    }

    public function indexAction()
    {
        $this->_redirect('attack/list');
    }
    
    public function listAction()
    {
    	$auth = Zend_Auth::getInstance();
       	if ($auth->hasIdentity()) 
        {
            $user_id = $auth->getIdentity()->id; 
        }
        else 
        {
        	$this->_redirect('authentication/login');
        }
        
    	$attackList = new Application_Model_AttackPages();
    	$attackList = $attackList->getAttackList($this->view->playerStats['power'], $user_id);
    	
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($attackList));
        $paginator->setItemCountPerPage(15)
        		  ->setCurrentPageNumber($this->_getParam('page',1));
        $this->view->paginator = $paginator;
		
        $page = $paginator->getCurrentPageNumber();
        $gotoPageForm = new Application_Form_Pagination();
       	$gotoPageForm->pageNumber->setValue($page);

       	$this->view->gotoPage = $gotoPageForm;
       	
    	if($this->getRequest()->isPost())
	    {
	    	if($gotoPageForm->isValid($_POST))
	    	{
	    		$gotoPageNumber = $gotoPageForm->getValue('pageNumber');
	    		if($gotoPageNumber > $paginator->count() )
	    		{
	    			$gotoPageNumber = $paginator->count();
	    		}
    			$paginator->setCurrentPageNumber($this->_getParam('page',$gotoPageNumber));

    			$redirector = $this->_helper->getHelper('Redirector');
	    		$redirector->gotoSimple('list',
                                       'attack',
                                       null,
                                       array('page' => $gotoPageNumber
                                             ));		
	    	}
	    }
        
    }

    public function playerAction()
    {	
    	$auth = Zend_Auth::getInstance();
       	if ($auth->hasIdentity()) 
        {
            $user_id = $auth->getIdentity()->id; 
        }
        else 
        {
        	$this->_redirect('authentication/login');
        }
        
		if( $_GET['p'] != null)
		{
    		$defender_id = $_GET['p'];
    	}
    	else
    	{
    		$this->_redirect('attack/list');
    	}
    	
    	$attackForm = new Application_Form_Battle();
    	$this->view->attackForm = $attackForm;

    	
		if( isset($_SESSION['attack_result']) )
		{
			//read
			$previousAttackResult = $_SESSION['attack_result'];
			$this->setResultsToView($previousAttackResult);	
			//unset for next attack
			unset($_SESSION['attack_result']);
		}
		
		
		$defenderProf = $this->getPlayerInformation($defender_id);
    	
    	$this->view->defenderName = $defenderProf['alias'];
    	$this->view->defenderRace = $defenderProf['race_name'];
    	$this->view->defenderLevel = $defenderProf['hero_level'];
		$this->view->defenderTotalUnits = $defenderProf['current_population'];
		$this->view->defenderPower = $defenderProf['power'];
		$this->view->defenderWealth = $defenderProf['wealth'];
		
		$offenderProf = $this->getPlayerInformation($user_id);
		
		$this->view->offenderName = $offenderProf['alias'];
    	$this->view->offenderRace = $offenderProf['race_name'];
    	$this->view->offenderLevel = $offenderProf['hero_level'];
		$this->view->offenderTotalUnits = $offenderProf['current_population'];
		$this->view->offenderPower = $offenderProf['power'];
		$this->view->offenderWealth = $offenderProf['wealth'];
		
				
		
		$url = 'attack/player'.'?p='.$defender_id;
		if($this->getRequest()->isPost())
		{
			if($attackForm->isValid($_POST))
		    {
				//call attack_player
				$nrOfRaids = $attackForm->getValue('numberOfRaids');
		    	$attackResult = $this->attackPlayer($nrOfRaids, $defender_id, $user_id);
		    	$_SESSION['attack_result']=$attackResult;
		    	
		    	$this->applyAttackResult($attackResult, $user_id, $defender_id,$nrOfRaids);
		    	$this->_redirect($url);
		    }
		}
	    
    }
    
	private function setResultsToView($attackResult)
	{
		$this->view->winnerAndGold = $attackResult->attackResultMessage;
		$this->view->offenderTotalUnitsLost = $attackResult->offenderUnitsLost->totalUnitsLost; 
		$this->view->defenderTotalUnitsLost = $attackResult->defenderUnitsLost->totalUnitsLost;
		$this->view->offenderTotalPowerLost = $attackResult->offenderUnitsLost->totalPowerLost; 
		$this->view->defenderTotalPowerLost = $attackResult->defenderUnitsLost->totalPowerLost;
			
		$this->view->offenderDamage = $attackResult->offenderDamage;
		$this->view->defenderDamage = $attackResult->defenderDamage;
		$this->view->offenderOffense = $attackResult->offenderOffense;
		$this->view->offenderDefense = $attackResult->offenderDefense;
		$this->view->defenderOffense = $attackResult->defenderOffense;
		$this->view->defenderDefense = $attackResult->defenderDefense;
		
		$this->view->a_xp = $attackResult->aXP;
		$this->view->d_xp = $attackResult->dXP;
		$this->view->a_1 = $attackResult->offenderUnitsLost->lvl1 + $attackResult->offenderUnitsLost->numberOfUnitsReincarnated;
		$this->view->a_2 = $attackResult->offenderUnitsLost->lvl2;
		$this->view->a_3 = $attackResult->offenderUnitsLost->lvl3;
		$this->view->a_4 = $attackResult->offenderUnitsLost->lvl4;
		$this->view->a_5 = $attackResult->offenderUnitsLost->lvl5;
		$this->view->a_6 = $attackResult->offenderUnitsLost->lvl6;
		
		$this->view->d_1 = $attackResult->defenderUnitsLost->lvl1;
		$this->view->d_2 = $attackResult->defenderUnitsLost->lvl2;
		$this->view->d_3 = $attackResult->defenderUnitsLost->lvl3;
		$this->view->d_4 = $attackResult->defenderUnitsLost->lvl4;
		$this->view->d_5 = $attackResult->defenderUnitsLost->lvl5;
		$this->view->d_6 = $attackResult->defenderUnitsLost->lvl6;
			
	}
	
    private function applyAttackResult($attackResult, $attacker_id, $defender_id, $nrOfRaids)
    {
		//A. attacker
   		//1. apply to units table: update casualties, reincarnation, calc powerLost, incomeLost, powerWin, incomeWin
		$raceUnits = new Application_Model_RaceUnits();
		$userStats = new Application_Model_UserStatsTbl();
		$playerProfile = new Application_Model_PlayerProfile();
    	//get player details: we need: power, off, deff, units_nr, units_power, units_health, units_distrib,

		
    	$playerInfo = $playerProfile->getPlayerArmyDetails($attacker_id);
		if(strtolower($playerInfo['class_name']) == 'light')
		{
			$lightArmy = new	Application_Model_LightUserArmyTbl();
			$row = $lightArmy->fetchRow($lightArmy->select()->where('user_fk = ?', $attacker_id));
			//Change the value of one or more column
			$row->lvl1 = $row->lvl1 - $attackResult->offenderUnitsLost->lvl1 + $attackResult->offenderUnitsLost->numberOfUnitsReincarnated;
			$row->lvl2 = $row->lvl2 - $attackResult->offenderUnitsLost->lvl2;
			$row->lvl3 = $row->lvl3 - $attackResult->offenderUnitsLost->lvl3;
			$row->lvl4 = $row->lvl4 - $attackResult->offenderUnitsLost->lvl4;
			$row->lvl5 = $row->lvl5 - $attackResult->offenderUnitsLost->lvl5;
			$row->lvl6 = $row->lvl6 - $attackResult->offenderUnitsLost->lvl6;
			$row->save();
		}
		else
		{
			$darkArmy = new	Application_Model_DarkUserArmyTbl();
			$row = $darkArmy->fetchRow($darkArmy->select()->where('user_fk = ?', $attacker_id));
			//Change the value of one or more column
			$row->lvl1 = $row->lvl1 - $attackResult->offenderUnitsLost->lvl1 + $attackResult->offenderUnitsLost->numberOfUnitsReincarnated;
			$row->lvl2 = $row->lvl2 - $attackResult->offenderUnitsLost->lvl2;
			$row->lvl3 = $row->lvl3 - $attackResult->offenderUnitsLost->lvl3;
			$row->lvl4 = $row->lvl4 - $attackResult->offenderUnitsLost->lvl4;
			$row->lvl5 = $row->lvl5 - $attackResult->offenderUnitsLost->lvl5;
			$row->lvl6 = $row->lvl6 - $attackResult->offenderUnitsLost->lvl6;
			$row->save();
		}
		
		
		$incomeResult = $raceUnits->getUnitsIncomeForRaceID($playerInfo['race_name']);
		$incomeLost  = 0;
		$incomeLost =  $incomeResult['lvl1']*$attackResult->offenderUnitsLost->lvl1+ 
					   $incomeResult['lvl2']*$attackResult->offenderUnitsLost->lvl2+
					   $incomeResult['lvl3']*$attackResult->offenderUnitsLost->lvl3+
					   $incomeResult['lvl4']*$attackResult->offenderUnitsLost->lvl4+ 
					   $incomeResult['lvl5']*$attackResult->offenderUnitsLost->lvl5+
					   $incomeResult['lvl6']*$attackResult->offenderUnitsLost->lvl6
					   -$incomeResult['lvl1']*$attackResult->offenderUnitsLost->numberOfUnitsReincarnated;
		
		$porwerLost = $attackResult->offenderUnitsLost->totalPowerLost;
		
		
		//2. apply to user_stats: power, income, xp, gold_win, wealth, ap, current_population
		
		$row = $userStats->fetchRow($userStats->select()->where('user_fk = ?', $attacker_id));
		$row->wealth += $attackResult->coinsWinBonus;
		$row->power  -= $porwerLost;
		$row->income -= $incomeLost;
		$row->current_population  -= $attackResult->offenderUnitsLost->totalUnitsLost; 
		$row->xp += $attackResult->aXP;
		$row->attack_points -= $nrOfRaids;
		$row->coins_earn += $attackResult->coinsWinBonus;
    	$row->save();
		
				
		//B. deffender
		//1. apply to units table: update casualties, calc powerLost, incomeLost
		
		$playerInfo = $playerProfile->getPlayerArmyDetails($defender_id);
		if(strtolower($playerInfo['class_name']) == 'light')
		{
			$lightArmy = new	Application_Model_LightUserArmyTbl();
			$row = $lightArmy->fetchRow($lightArmy->select()->where('user_fk = ?', $defender_id));
			//Change the value of one or more column
			$row->lvl1 = $row->lvl1 - $attackResult->defenderUnitsLost->lvl1;
			$row->lvl2 = $row->lvl2 - $attackResult->defenderUnitsLost->lvl2;
			$row->lvl3 = $row->lvl3 - $attackResult->defenderUnitsLost->lvl3;
			$row->lvl4 = $row->lvl4 - $attackResult->defenderUnitsLost->lvl4;
			$row->lvl5 = $row->lvl5 - $attackResult->defenderUnitsLost->lvl5;
			$row->lvl6 = $row->lvl6 - $attackResult->defenderUnitsLost->lvl6;
			$row->save();
		}
		else
		{
			$darkArmy = new	Application_Model_DarkUserArmyTbl();
			$row = $darkArmy->fetchRow($darkArmy->select()->where('user_fk = ?', $defender_id));
			//Change the value of one or more column
			$row->lvl1 = $row->lvl1 - $attackResult->defenderUnitsLost->lvl1;
			$row->lvl2 = $row->lvl2 - $attackResult->defenderUnitsLost->lvl2;
			$row->lvl3 = $row->lvl3 - $attackResult->defenderUnitsLost->lvl3;
			$row->lvl4 = $row->lvl4 - $attackResult->defenderUnitsLost->lvl4;
			$row->lvl5 = $row->lvl5 - $attackResult->defenderUnitsLost->lvl5;
			$row->lvl6 = $row->lvl6 - $attackResult->defenderUnitsLost->lvl6;
			$row->save();
		}
		
		
		$incomeResult = $raceUnits->getUnitsIncomeForRaceID($playerInfo['race_name']);
		$incomeLost  = 0;
		$incomeLost =  $incomeResult['lvl1']*$attackResult->defenderUnitsLost->lvl1+ 
					   $incomeResult['lvl2']*$attackResult->defenderUnitsLost->lvl2+
					   $incomeResult['lvl3']*$attackResult->defenderUnitsLost->lvl3+
					   $incomeResult['lvl4']*$attackResult->defenderUnitsLost->lvl4+ 
					   $incomeResult['lvl5']*$attackResult->defenderUnitsLost->lvl5+
					   $incomeResult['lvl6']*$attackResult->defenderUnitsLost->lvl6;
		
		$porwerLost = $attackResult->defenderUnitsLost->totalPowerLost;
		
		
		//2. apply to user_stats: power, income, xp, gold_lost, wealth, current_population
		
		$row = $userStats->fetchRow($userStats->select()->where('user_fk = ?', $defender_id));
		$row->wealth -= $attackResult->coinsWinBonus;
		$row->power  -= $porwerLost;
		$row->income -= $incomeLost;
		$row->current_population  -= $attackResult->defenderUnitsLost->totalUnitsLost; 
		$row->xp += $attackResult->dXP;
		$row->coins_lost += $attackResult->coinsWinBonus;
    	$row->save();
    }
    
    private function attackPlayer($nrOfRaids, $defender_id, $user_id)
    {
    	
    	$attackResult = new stdClass();
    	
    	$defenderProf = $this->getPlayerInformation($defender_id);
    	$offenderProf = $this->getPlayerInformation($user_id);
    	    	
    	$attackResult = $this->getAttackResults($defenderProf, $offenderProf, $nrOfRaids);
    
    	//1. insert into reports attacking result
    	$reportsTbl = new Application_Model_ReportsTbl();		
		if ($attackResult->winner == $user_id){
			$result = "win"; 
		}else{
			$result = "lost";
		}
	
		$data = array(
				'attacker_id' => $user_id, 
				'defender_id' => $defender_id, 
				'nr_of_raids' => $nrOfRaids, 
				'result' => $result, 
				'coins' => $attackResult->coinsWinBonus, 
				'a_xp' => $attackResult->aXP, 
				'd_xp' => $attackResult->dXP, 
				'a_units_lost' => $attackResult->offenderUnitsLost->totalUnitsLost,
				'a_power_lost' => $attackResult->offenderUnitsLost->totalPowerLost, 
				'd_units_lost' => $attackResult->defenderUnitsLost->totalUnitsLost, 
				'd_power_lost' => $attackResult->defenderUnitsLost->totalPowerLost,
				'attacker_damage' =>$attackResult->offenderDamage, 
				'defender_damage' =>$attackResult->defenderDamage,
				'a_lvl1' =>  $attackResult->offenderUnitsLost->lvl1,
				'a_lvl2' =>  $attackResult->offenderUnitsLost->lvl2,
				'a_lvl3' =>  $attackResult->offenderUnitsLost->lvl3,
				'a_lvl4' =>  $attackResult->offenderUnitsLost->lvl4,
				'a_lvl5' =>  $attackResult->offenderUnitsLost->lvl5,
				'a_lvl6' =>  $attackResult->offenderUnitsLost->lvl6,
				'd_lvl1' =>  $attackResult->defenderUnitsLost->lvl1,
				'd_lvl2' =>  $attackResult->defenderUnitsLost->lvl2,
				'd_lvl3' =>  $attackResult->defenderUnitsLost->lvl3,
				'd_lvl4' =>  $attackResult->defenderUnitsLost->lvl4,
				'd_lvl5' =>  $attackResult->defenderUnitsLost->lvl5,
				'd_lvl6' =>  $attackResult->defenderUnitsLost->lvl6
				);
		$reportsTbl->insert($data);
		return $attackResult;
    	
    }
    
    //IMPORTANT: ATTACK PARRAMS ARE CALCULATED FOR 10 RAIDS. SHOULD BE ADJUSTED TO NR_OF_RAIDS SELECTED BY PLAYER WHEN DAMAGE ITS SPREAD
    private function getPlayerInformation($user_id)
    {
    	$playerProfile = new Application_Model_PlayerProfile();
    	//get player details: we need: power, off, deff, units_nr, units_power, units_health, units_distrib,
    	
    	$playerInfo = $playerProfile->getPlayerArmyDetails($user_id);
    	//we obtain:
    	//id, user_fk, power, attack_points, wealth, income, gold_earn, gold_lost, 
		//hero_level, turns_played, current_population, total_population
    	
    	$totalArmy =  $playerInfo['lvl1']+ $playerInfo['lvl2']+$playerInfo['lvl3']+$playerInfo['lvl4']+ $playerInfo['lvl5']+$playerInfo['lvl6'];
    	
    	if( $totalArmy != $playerInfo['current_population'] )
    	{
    		//todo update user_stats;
    	}
    	
    	$playerInfo['dis_l1']= round($playerInfo['lvl1']/$totalArmy*100);
    	$playerInfo['dis_l2']= round($playerInfo['lvl2']/$totalArmy*100);
    	$playerInfo['dis_l3']= round($playerInfo['lvl3']/$totalArmy*100);
    	$playerInfo['dis_l4']= round($playerInfo['lvl4']/$totalArmy*100);
    	$playerInfo['dis_l5']= round($playerInfo['lvl5']/$totalArmy*100);
    	$playerInfo['dis_l6']= round($playerInfo['lvl6']/$totalArmy*100);
    	
    	$totalPower = $playerInfo['p1']+ $playerInfo['p2']+$playerInfo['p3']+$playerInfo['p4']+$playerInfo['p5'];
    	$powerAverage = round($totalPower/5,1);
    	$hpBonusDef = $powerAverage*100;
    	
    	$playerInfo['hp_l1']= $hpBonusDef + $playerInfo['p1']*100;
    	$playerInfo['hp_l2']= $hpBonusDef + $playerInfo['p2']*200;
    	$playerInfo['hp_l3']= $hpBonusDef + $playerInfo['p3']*300;
    	$playerInfo['hp_l4']= $hpBonusDef + $playerInfo['p4']*400;
    	$playerInfo['hp_l5']= $hpBonusDef + $playerInfo['p5']*500;
    	$playerInfo['hp_l6']= $hpBonusDef + $playerInfo['p6']*600;
    	
    	//Total offence = offense + offense* racial bonus 
    	$playerInfo['offence']=0;
    	$playerInfo['defence']=0;
    	$playerInfo['threshold']=$playerInfo['power']*0.4;  	
    	
//    	srand($this->make_seed());
//    	$playerInfo['damage_no_bonus']=rand($playerInfo['power']*0.4,$playerInfo['power']*0.8);
//    	$playerInfo['damage']=$playerInfo['damage_no_bonus'] + $playerInfo['offence'] - $playerInfo['defence'];
//    	$playerInfo['threshold']=$playerInfo['power']*0.4;
    	return $playerInfo;
    	
    }
    private function calculateDamageWithRacialBonus($playerInfo)
    {
    	$damage = 0;
    	$racialBonusLevel = 0;
    	
    	if( strtolower($playerInfo['race_name']) == 'elf' )
    	{
    		$racialBonusLevel = 0;
    	}
    	
    	if( strtolower($playerInfo['race_name']) == 'dwarf' )
    	{
    		$racialBonusLevel = 0;
    	}
    	
    	$offence = $playerInfo['offence'] + $playerInfo['offence']*$racialBonusLevel;
    	$defence = $playerInfo['defence'] + $playerInfo['defence']*$racialBonusLevel;
    	
    	srand($this->make_seed());  	
    	$damage_no_bonus = rand($playerInfo['power']*0.4,$playerInfo['power']*0.8);
    	$damage =  $damage_no_bonus + $offence - $defence;
    	
    	return $damage;
    }
    
	private function calculateCoinsTaken($playerProf, $nrOfRaids)
    {
    	$coinsWin = 0;
    	srand($this->make_seed());
    	$coinsWin = rand($playerProf['wealth']*0.3,$playerProf['wealth']*0.5)/10*$nrOfRaids;
    	$coinsWin = round($coinsWin);
    	return $coinsWin;
    }
    
    private function calculateCoinsTakenWithRacialBonus($playerProf, $coinsTaken)
    {
    	$coinWin = 0;
    	$racialBonusLevel = 0;
    	
    	if( strtolower($playerProf['race_name']) == 'orc' )
    	{
    		//eg: 5%  = 0.05
    		$racialBonusLevel = 0;
    	}
    	
    	if( strtolower($playerProf['race_name']) == 'troll' )
    	{
    		//eg: 10%  = -0.10
    		$racialBonusLevel = 0;
    	}
    	
    	$coinWin = $coinsTaken + $coinsTaken*$racialBonusLevel;
    	return $coinWin;
    }
    
    
    private function getAttackResults($defenderProf, $offenderProf, $nrOfRaids)
    {
    	
    	$attackResult = new stdClass();
    	$attackResult->attackerAlias = $offenderProf['alias'];
    	$attackResult->defenderAlias = $defenderProf['alias'];
    	
    	$attackerDamage = $this->calculateDamageWithRacialBonus($offenderProf);
    	$defenderDamage = $this->calculateDamageWithRacialBonus($defenderProf);
    	
    	$coinsTaken = $this->calculateCoinsTaken($defenderProf,$nrOfRaids);
    	$coinsTakenWithRacialBonus = $this->calculateCoinsTakenWithRacialBonus($defenderProf,$coinsTaken);
    	    	   	
    	// ->totalUnitsLost ->totalPowerLost , ->lvl1...6 
    	$attackResult->defenderUnitsLost = $this->getUnitsLost($attackerDamage,  $offenderProf['threshold'], $defenderProf, $nrOfRaids);
    	$attackResult->offenderUnitsLost = $this->getUnitsLost($defenderDamage,  $defenderProf['threshold'], $offenderProf, $nrOfRaids);
    	
    	$numberOfUnitsReincarnated = 0;
    	if( strtolower($offenderProf['race_name']) == 'undead' )
    	{
    		//eg 10% = 0.10 of killing units will be extra lvl1 units for him
    		$racialBonus = 0.10;
    		$numberOfUnitsReincarnated = $racialBonus * $attackResult->defenderUnitsLost->totalUnitsLost; 
    	}
    	
    	$attackResult->offenderUnitsLost->numberOfUnitsReincarnated = $numberOfUnitsReincarnated;
    	
    	if ( $attackerDamage > $defenderDamage )
    	{
    		$attackResult->winner = $offenderProf['user_fk'];
    		$attackResult->coinsWin = $coinsTaken;
    		$attackResult->coinsWinBonus = $coinsTakenWithRacialBonus;
    		if ($coinsTakenWithRacialBonus != $coinsTaken )
    		{
    			$attackResult->attackResultMessage = 'You win '. $coinsTaken. ' coins!<br/>Bonus coins win from strike is '. $coinsTakenWithRacialBonus;
    		}	
    		$attackResult->attackResultMessage = 'You win '. $coinsTaken. ' coins!';
    	}
   	
    	else 
    	{
    		$attackResult->winner = $defenderProf['user_fk'];
    		$attackResult->attackResultMessage = 'You lost!';
    		$attackResult->coinsWin = 0;
    		$attackResult->coinsWinBonus = 0;
    	}
    	
    	$attackResult->aXP = $attackerDamage*0.06;
    	if($defenderDamage > 0)
    	{
    		$attackResult->dXP = $defenderDamage*0.06;
    	}
    	else
    	{
    		$attackResult->dXP = 0;
    	}
    	$attackResult->winnerXP = $attackResult->aXP; 
    	$attackResult->loserXP = $attackResult->dXP;
    	
    	
    	$attackResult->offenderDamage = $attackerDamage;
    	$attackResult->defenderDamage = $defenderDamage;
    	$attackResult->offenderOffense = $offenderProf['offence'];
    	$attackResult->offenderDefense = $offenderProf['defence'];
		$attackResult->defenderOffense = $defenderProf['offence'];
    	$attackResult->defenderDefense = $defenderProf['defence'];

    	return $attackResult;
    }

    private function getUnitsLost($damage, $threshold, $playerProf, $nrOfRaids)
    {
    	$unitsLost = new stdClass();
    	
    	if( $damage < $threshold )
    	{
    		$unitsLost = 0;
    		$unitsLost->totalUnitsLost = 0;
	    	$unitsLost->totalPowerLost = 0;
    	}
    	else 
    	{
	    	//he lose units
	    	$unitsLost->damage_lvl1 = $playerProf['dis_l1']* $damage / 100;
	    	$unitsLost->damage_lvl2 = $playerProf['dis_l2']* $damage / 100;
	    	$unitsLost->damage_lvl3 = $playerProf['dis_l3']* $damage / 100;
	    	$unitsLost->damage_lvl4 = $playerProf['dis_l4']* $damage / 100;
	    	$unitsLost->damage_lvl5 = $playerProf['dis_l5']* $damage / 100;
	    	$unitsLost->damage_lvl6 = $playerProf['dis_l6']* $damage / 100;
	    	
	    	
	    	//  / 10 * $nrOfRaids because power of attack is it for 10 raids
	    	$unitsLost->lvl1 = round($unitsLost->damage_lvl1 /  $playerProf['hp_l1'] / 10 * $nrOfRaids);
	    	$unitsLost->lvl2 = round($unitsLost->damage_lvl2 /  $playerProf['hp_l2'] / 10 * $nrOfRaids);
	    	$unitsLost->lvl3 = round($unitsLost->damage_lvl3 /  $playerProf['hp_l3'] / 10 * $nrOfRaids);
	    	$unitsLost->lvl4 = round($unitsLost->damage_lvl4 /  $playerProf['hp_l4'] / 10 * $nrOfRaids);
	    	$unitsLost->lvl5 = round($unitsLost->damage_lvl5 /  $playerProf['hp_l5'] / 10 * $nrOfRaids);
	    	$unitsLost->lvl6 = round($unitsLost->damage_lvl6 /  $playerProf['hp_l6'] / 10 * $nrOfRaids);
	    	
	    	$unitsLost->totalUnitsLost = $unitsLost->lvl1+ $unitsLost->lvl2 + $unitsLost->lvl3 + $unitsLost->lvl4+ $unitsLost->lvl5 + $unitsLost->lvl6;
	    	$unitsLost->totalPowerLost = $unitsLost->lvl1*$playerProf['p1']+ 
								    	$unitsLost->lvl2*$playerProf['p2']+ 
								    	$unitsLost->lvl3*$playerProf['p3']+ 
								    	$unitsLost->lvl4*$playerProf['p4']+ 
								    	$unitsLost->lvl5*$playerProf['p5']+ 
								    	$unitsLost->lvl6*$playerProf['p6'];
    	}   
    	return $unitsLost;
    }
	private function make_seed()
	{
		list($usec, $sec) = explode(' ', microtime());
		return (float) $sec + ((float) $usec * 100000);
	}
}







